package com.wind.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @author: jishengping
 * @Date: 2019-10-20
 * @Time: 上午1:00
 */
@RestController
public class CpuController {

    /**
     * 死循环
     * */
    @RequestMapping("/loop")
    public String loop(){
        return this.doLoop();
    }

    private String doLoop(){
        while (true){

        }
    }


    private Object lock1 = new Object();
    private Object lock2 = new Object();
    /**
     * 死锁
     * */
    @RequestMapping("/deadlock")
    public String deadlock(){
        new Thread(()->{
            synchronized(lock1) {
                try {Thread.sleep(1000);}catch(Exception e) {}
                synchronized(lock2) {
                    System.out.println("Thread1 over");
                }
            }
        }) .start();
        new Thread(()->{
            synchronized(lock2) {
                try {Thread.sleep(1000);}catch(Exception e) {}
                synchronized(lock1) {
                    System.out.println("Thread2 over");
                }
            }
        }) .start();
        return "deadlock";
    }

}
